import * as Cesium from "cesium";
import { default as proj4 } from "proj4";

// 获取list对应值key的下标
export function getIndex(key, list) {
	return list.indexOf(key);
}

// 根据经纬度计算两点之间的距离
export function getDistance(point1, point2) {
	point1 = tranformCoordinate(point1[0], point1[1]);
	point2 = tranformCoordinate(point2[0], point2[1]);
	let startPosition = new Cesium.Cartesian3.fromDegrees(point1[0], point1[1]);
	let endPosition = new Cesium.Cartesian3.fromDegrees(point2[0], point2[1]);
	let distance = Cesium.Cartesian3.distance(startPosition, endPosition);
	return distance;
}

// 根据起点startHeight高度和终点endHeight高度填充中间点高度（等差数列）
export function getHeights(startHeight = 0, endHeight = 0, arr) {
	let startNode = arr[0];
	let endNode = arr[arr.length - 1];
	let total_height = endHeight - startHeight;
	let total_distance = getDistance(startNode, endNode);
	let res = [];
	for (let i = 0; i <= arr.length - 1; i++) {
		let distance = getDistance(startNode, arr[i]);
		res.push(startHeight + total_height * (distance / total_distance));
	}
	return res;
}

// 根据某点到起点距离赋不同颜色值
export function getColors(arr, total_length, single) {
	let ratio = 1000;
	let startNode = arr[0];
	let endNode = arr[arr.length - 1];
	let total_distance = getDistance(startNode, endNode);
	let res = [];
	for (let index = 0; index <= arr.length - 1; index++) {
		let distance = getDistance(startNode, arr[index]);
		let length = total_length * (distance / total_distance);
		let color = new Cesium.Color(length / ratio, total_length / ratio, 0.099, 0);
		if (single) {
			color = new Cesium.Color(length / ratio, total_length / ratio, 1, 0);
		}
		res.push(color);
	}
	return res;
}

// 将arr和heights的数据格式转为cesium的数据格式
export function handleFromDegreesArray(data, height) {
	let arr = [];
	data.forEach(item => {
		arr.push(...tranformCoordinate(item[0], item[1]));
		if (height !== undefined) arr.push(height);
	});
	return arr;
}

export function handleFromDegreesArrayHeight(data, height, offset) {
	let arr = [];
	data.forEach((item, index) => {
		arr.push(...tranformCoordinate(item[0], item[1]));
		arr.push(offset ? height[index] + offset : height[index]);
	});
	return arr;
}

// 根据流量排序（一维数组排序）
export function handleSortList(arr, flow = 0, interval = 1) {
	let res = [];
	if (Number(flow) < 0) {
		for (let i = 0; i <= arr.length - 1; i += interval) {
			res.unshift(...arr.slice(i, i + interval));
		}
	} else {
		res = arr;
	}
	return res;
}

// 根据流量排序（二维数组排序）
export function handleSortArr(arr, flow = 0) {
	let res = Number(flow) < 0 ? arr.reverse() : arr;
	return res;
}

// EPSG:2326坐标转WGS 84坐标
export function tranformCoordinate(x, y) {
	const fromProjection = `PROJCS["Hong Kong 1980 Grid System",GEOGCS["Hong Kong 1980",DATUM["Hong_Kong_1980",SPHEROID["International 1924",6378388,297],TOWGS84[-162.619,-276.959,-161.764,-0.067753,2.243648,1.158828,-1.094246]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4611"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",22.3121333333333],PARAMETER["central_meridian",114.178555555556],PARAMETER["scale_factor",1],PARAMETER["false_easting",836694.05],PARAMETER["false_northing",819069.8],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","2326"]]`;
	const toProjection = `GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]`;
	const newCoordinates = proj4(fromProjection, toProjection, [x, y]);
	return [newCoordinates[0] - 0.0002, newCoordinates[1] - 0.0005]; // 返回数组格式
}

// 根据传参获取时间的字符串格式
export function formatDateByProp(date, fmt) {
	date = new Date(date);
	let ret;
	const opt = {
		"[Yy]+": date.getFullYear().toString(), // 年
		"M+": (date.getMonth() + 1).toString(), // 月
		"[Dd]+": date.getDate().toString(), // 日
		"[Hh]+": date.getHours().toString(), // 时
		"m+": date.getMinutes().toString(), // 分
		"[Ss]+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"));
		}
	}
	return fmt;
}
